package com.baosight.sczx.zm.ch.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cg.DailyReport;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlanJJ;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ch.domain.ZMCHGrubReport;

/**
 * 掘进日上报
 * <p>Title: ServiceZMCP95.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-9-6 上午06:30:28  严洪浩  Create
 *</pre>
 */
public class ServiceZMCH10 extends ServiceEPBase {
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午06:31:00 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的 
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午06:31:28 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {			
		String fdMiningId = (String)UserSession.getInSessionProperty("miningCode");
	    String fdDepartmentId = (String)UserSession.getInSessionProperty("orgCode");
	    
	    if(fdDepartmentId != null && fdDepartmentId.length() > 9) {
	    	fdDepartmentId = fdDepartmentId.substring(0, 9);
	    }
	    
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		StaticTools.setQueryParameters(inInfo, "fdReportDepartmentId", fdDepartmentId, true);
		
		EiInfo outInfo = super.query(inInfo, "ZMCHGrubReport.query", new ZMCHGrubReport());
		
		String year  = inInfo.getBlock("inqu_status").getCellStr(0, "fdCurrentDate") == null ? StaticTools.getYear() : inInfo.getBlock("inqu_status").getCellStr(0, "fdCurrentDate");

		outInfo.addBlock(ProducePlanJJ.getRoadWayBlock("cmes.ch.roadway", fdMiningId,StaticTools.getYearStr(year), null, fdDepartmentId, true));
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum",fdMiningId, true));//班次
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[]{"草拟", "上报"}, new String[]{"草拟", "上报"}, true));//状态
		
		return outInfo;
	}
	
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:05:43 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo();
		ZMCHGrubReport _dom = new ZMCHGrubReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_GRUBREPORT"));//主键
			
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
			_dom.setFdReportDepartmentId(UserSession.getInSessionProperty("orgCode").toString());// 汇报部门id
			_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());// 汇报部门
			_dom.setFdReporterId(UserSession.getUserId().toString());// 汇报人ID
			_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());// 汇报人
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
			
			boolean validate = validate(inInfo, _dom, "ZMCHGrubReport.validate", new HashMap<String, String>());
			if (validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			this.dao.insert("ZMCHGrubReport.insert", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;	
	}
		
	
	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:09:01 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCHGrubReport _dom = new ZMCHGrubReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = validate(inInfo, _dom, "ZMCHGrubReport.validate", params);
			if(validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			else {
				_dom.setFdReporterId(UserSession.getUserId().toString());// 汇报人ID
				_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());// 汇报人
				_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
				this.dao.update("ZMCHGrubReport.update", _dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;	
	}
	

	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:35 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCHGrubReport _dom = new ZMCHGrubReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCHGrubReport.delete", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   

	}
	
	/**
	 * 上报
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:12:20 严洪浩
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{	
		EiInfo out=new EiInfo(); 
		ZMCHGrubReport _dom = new ZMCHGrubReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> map = new HashMap<String, String>();
			map.put("fdCurrentDate", _dom.getFdCurrentDate());
			map.put("fdReportDepartmentId", _dom.getFdReportDepartmentId());
			map.put("fdWorknumId", _dom.getFdWorknumId());
			map.put("fdProjectId", _dom.getFdProjectId());
			
			boolean validate = DailyReport.validate("ZMCGGrubReportFinal.query", map);
			if(validate) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("已经存在数据，无需上报！");
				return out; 
			}
			else {
				int result = dao.update("ZMCHGrubReport.report", _dom);
				if(result > 0){
					if(_dom.getFdReportDepartmentId() != null) {
						String fdReportDepartmentId = _dom.getFdReportDepartmentId().substring(0, 9);
						_dom.setFdReportDepartmentId(fdReportDepartmentId);
						_dom.setFdReportDepartment(Department.getInstance().getDepartmentNameById(fdReportDepartmentId));
					}
				
					DailyReport.copy("ZMCGGrubReportFinal.insert", _dom.toMap());
				}
			}
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}
	

	/**
	 * 验证是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:04:59 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCHGrubReport _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		params.put("fdWorknumId", _dom.getFdWorknumId());
		params.put("fdMiningId", _dom.getFdMiningId());
		params.put("fdCurrentDate", _dom.getFdCurrentDate());
		params.put("fdReportDepartmentId", _dom.getFdReportDepartmentId());
		params.put("fdProjectId", _dom.getFdProjectId());
		
		List result = dao.query("ZMCHGrubReport.validate", params);

		if (result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "掘进进度汇报每单位每天每班只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
}
